package com.tutorialspoint.test.UIUtils;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;

@ManagedBean(name="uiUtils", eager=true)
public class UIUtils implements Serializable {

	private static final long serialVersionUID = -1305745051100989275L;
	private int viewLoadCount = 0;

	public void greetOnViewLoad(ComponentSystemEvent event) {
		FacesContext context = FacesContext.getCurrentInstance();

		if (viewLoadCount < 1 && !context.isPostback()) {
			String userName = (String) event.getComponent().getAttributes()
					.get("userName");

			FacesMessage message = new FacesMessage(String.format(
					"Welcome to your account %s", userName));
			context.addMessage("growlMessages", message);

			viewLoadCount++;
		}
	}

}
